<template>
  <div class="satisfaction">
    <module
      ref="module"
      :moduleType="channelDistribute.moduleType"
      :borderConfig="channelDistribute.borderConfig"
      :moduleConfig="channelDistribute.moduleConfig"></module>
    <module
      ref="module1"
      :moduleType="typeDistribute.moduleType"
      :borderConfig="typeDistribute.borderConfig"
      :moduleConfig="typeDistribute.moduleConfig"></module>
    <module
      ref="module2"
      :borderConfig="complaintMonth.borderConfig"
      :moduleConfig="complaintMonth.moduleConfig"></module>
    <module
      ref="module3"
      :borderConfig="satisfactionMonth.borderConfig"
      :moduleConfig="satisfactionMonth.moduleConfig"></module>
    <module
      ref="module4"
      :borderConfig="complaintOrgan.borderConfig"
      :moduleConfig="complaintOrgan.moduleConfig"
      :isShowDimension="complaintOrgan.isShowDimension"></module>
    <module
      ref="module5"
      :borderConfig="satisfactionOrgan.borderConfig"
      :moduleConfig="satisfactionOrgan.moduleConfig"
      :isShowDimension="satisfactionOrgan.isShowDimension"></module>
  </div>
</template>

<script>
import module from '../components/SecondaryModule'
import { changeBarData, separateData } from './../../utils/index'
export default {
  components: {
    module
  },
  computed: {
    organId () {
      return this.$store.state.organId
    }
  },
  name: 'satisfaction',
  data () {
    return {
      // 本月投诉渠道分布配置
      channelDistribute: {
        moduleType: 'pie',
        borderConfig: {
          moduleType: '2x1',
          icon: '',
          titleText: '本月投诉渠道',
          ref: 'channelDistribute'
        },
        moduleConfig: {
          optionsConfig: {
            isUseValue: true,
            value: [],
            labelLine: {
              normal: {
                length: 30,
                length2: 20
              }
            },
            legend: []
          },
          legendList: [],
          legendDirection: 'vertical'
        }
      },
      // 本月投诉类型分布配置
      typeDistribute: {
        moduleType: 'pie',
        borderConfig: {
          moduleType: '2x1',
          icon: '',
          titleText: '本月投诉类型',
          ref: 'typeDistribute'
        },
        moduleConfig: {
          optionsConfig: {
            isUseValue: true,
            labelLine: {
              normal: {
                length: 30,
                length2: 20
              }
            },
            value: [],
            legend: []
          },
          legendList: [],
          legendDirection: 'vertical'
        }
      },
      // 客户投诉同比&环比配置
      complaintMonth: {
        borderConfig: {
          titleText: '客户投诉趋势',
          moduleType: '2x1',
          ref: 'complaintMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              isNamePreposition: false,
              tipsMessage: '客户投诉'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 客户满意率同比&环比配置
      satisfactionMonth: {
        borderConfig: {
          titleText: '客户满意率趋势',
          moduleType: '2x1',
          ref: 'satisfactionMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '客户满意率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 客户投诉组织机构对比配置
      complaintOrgan: {
        borderConfig: {
          titleText: '客户投诉对比',
          moduleType: '2x1',
          ref: 'complaintOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisNameLength: 8
              // yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              // symbol: '万元',
              // tipsColor: ['#b60d81'],
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '60%'
            },
            seriesData: []
          }
        },
        isShowDimension: true
      },
      // 客户满意率组织机构对比配置
      satisfactionOrgan: {
        borderConfig: {
          titleText: '客户满意率对比',
          moduleType: '2x1',
          ref: 'satisfactionOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              tipsColor: ['#b60d81'],
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '60%'
            },
            seriesData: []
          }
        },
        isShowDimension: true
      }
    }
  },
  methods: {
    showTips (data) {
      this.$parent.showTips(data)
    },
    // 改变时间维度时
    changeDimension (obj) {
      switch (obj.ref) {
        case 'complaintOrgan': this.getBranchComplainCountList(obj.index + 1)
          break
        case 'satisfactionOrgan': this.getBranchSatisfiedRateList(obj.index + 1)
          break
        default: break
      }
    },
    // 请求本月投诉渠道分布配置
    getComplainChannelList () {
      this.orderApi.getComplainChannelList({ organId: this.organId }).then(res => {
        let value = []
        let legend = []
        let legendList = []
        let data = []
        if (res.data.code === '0') {
          data = res.data.data
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        data.forEach((item, index) => {
          value.push(item.count)
          legend.push(item.channelName)
          legendList.push(item.channelName)
        })
        if (legendList.length > 8) {
          legendList.splice(7, legendList.length - 7, '其余类型')
        }
        this.channelDistribute.moduleConfig.optionsConfig.value = value
        this.channelDistribute.moduleConfig.optionsConfig.legend = legend
        this.channelDistribute.moduleConfig.legendList = legendList
      }, res => {
        this.channelDistribute.moduleConfig.optionsConfig.value = []
        this.channelDistribute.moduleConfig.optionsConfig.legend = []
        this.channelDistribute.moduleConfig.legendList = []
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 获取投诉类型分布
    getComplainTypeList () {
      this.orderApi.getComplainTypeList({ organId: this.organId }).then(res => {
        let value = []
        let legend = []
        let legendList = []
        let data = []
        if (res.data.code === '0') {
          data = res.data.data
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        data.forEach((item, index) => {
          value.push(item.count)
          legend.push(item.typeName)
          legendList.push(item.typeName)
        })
        if (legendList.length > 8) {
          legendList.splice(7, legendList.length - 7, '其余类型')
        }
        this.typeDistribute.moduleConfig.optionsConfig.value = value
        this.typeDistribute.moduleConfig.optionsConfig.legend = legend
        this.typeDistribute.moduleConfig.legendList = legendList
      }, res => {
        this.typeDistribute.moduleConfig.optionsConfig.value = []
        this.typeDistribute.moduleConfig.optionsConfig.legend = []
        this.typeDistribute.moduleConfig.legendList = []
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 客户投诉同比环比
    getComplainYearOnYearList () {
      this.orderApi.getComplainYearOnYearList({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data)
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.complaintMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.complaintMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 客户满意度同比环比
    getSatisfiedRateYearOnYearList () {
      this.orderApi.getSatisfiedRateYearOnYearList({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'rate')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.satisfactionMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
      }, res => {
        this.satisfactionMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 客户投诉分公司对比
    getBranchComplainCountList (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.orderApi.getBranchComplainCountList({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let name = []
        if (res.data.code === '0') {
          data = separateData(res.data.data, 'count').data
          name = separateData(res.data.data, 'count').tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.complaintOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.complaintOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            name: '客户投诉',
            colorIndex: 0,
            isBar: true
          }
        ]
      }, res => {
        this.complaintOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.complaintOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: '客户投诉',
            colorIndex: 0,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 客户满意度分公司对比
    getBranchSatisfiedRateList (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.orderApi.getBranchSatisfiedRateList({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let name = []
        if (res.data.code === '0') {
          data = separateData(res.data.data, 'rate').data
          name = separateData(res.data.data, 'rate').tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.satisfactionOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.satisfactionOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            name: '客户满意率',
            colorIndex: 0,
            isBar: true
          }
        ]
      }, res => {
        this.satisfactionOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.satisfactionOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: '客户满意率',
            colorIndex: 0,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 请求数据
    getData () {
      this.getComplainChannelList()
      this.getComplainTypeList()
      this.getComplainYearOnYearList()
      this.getSatisfiedRateYearOnYearList()
      this.getBranchComplainCountList()
      this.getBranchSatisfiedRateList()
    },
    resizeChart () {
      this.$refs.module.resizeChart()
      this.$refs.module1.resizeChart()
      this.$refs.module2.resizeChart()
      this.$refs.module3.resizeChart()
      this.$refs.module4.resizeChart()
      this.$refs.module5.resizeChart()
    }
  },
  mounted () {
    // this.getData()
  }
}
</script>

<style lang="scss" scoped>
  .satisfaction{
    width: calc(100% - 10px);
    position: relative;
    margin: 5px;
    overflow: hidden;
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
</style>
